/*
 * @copyright fxsen <br/>
 * @date 2022/4/25 22:32 <br/>
 * @email <a href="mailto:fxsen@foxmail.com">
 * @author fxsen
 */
package com.fxsen.platform.web.controllers;

import com.fxsen.commons.core.annotation.RecordLog;
import com.fxsen.commons.core.annotation.RequiredPermission;
import com.fxsen.commons.core.base.result.ResponseResult;
import com.fxsen.commons.core.constant.Constants;
import com.fxsen.commons.sysmanager.core.entity.DictionaryDetail;
import com.fxsen.commons.sysmanager.core.entity.vo.DictionaryKeyValue;
import com.fxsen.commons.sysmanager.core.service.IDictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;

/**、
 * 字典详情控制器
 * @author fxsen
 * @date 2022/4/25 22:32
 * @email <a href="mailto:fxsen@foxmail.com">
 */
@RestController
@RequestMapping("/dictionaryDetail")
@RequiredPermission("dictionaryDetail")
@Api(tags = "系统：字典详情接口")
public class DictionaryDetailController {
    @Autowired
    private IDictionaryDetailService dictionaryDetailService;
    @ApiOperation(value = "新增")
    @PostMapping("save")
    @RequiredPermission("dictionaryDetail:save")
    @RecordLog("新增字典详情")
    public ResponseResult save(@RequestBody DictionaryDetail detail){
        try {
            if (detail == null || StringUtils.isBlank(detail.getDictionaryId())) {
                return ResponseResult.failed("未传请求参数！");
            }
            String name = detail.getName();
            String pid = detail.getPid();
            if (StringUtils.isBlank(name)){
                return ResponseResult.failed("名称不能为空！");
            }
            if (dictionaryDetailService.isExist("name",name)){
                return ResponseResult.failed("名称”"+name+"“已存在！");
            }
            //检查所添加的上级数据是否存在
            if (StringUtils.isNotBlank(pid)){
                DictionaryDetail dictionaryDetail = dictionaryDetailService.getById(pid);
                if (dictionaryDetail == null) {
                    return ResponseResult.failed("所添加的上级字典不存在！");
                }
            }else {
                detail.setPid(Constants.PARENT_ID);
            }
            boolean save = dictionaryDetailService.save(detail);
            return save ? ResponseResult.success() : ResponseResult.failed();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    @ApiOperation(value = "更新")
    @PostMapping("update")
    @RequiredPermission("dictionaryDetail:update")
    @RecordLog("更新字典详情")
    public ResponseResult update(@RequestBody DictionaryDetail detail){
        try {
            if (detail == null || StringUtils.isBlank(detail.getId()) || StringUtils.isBlank(detail.getDictionaryId())) {
                return ResponseResult.failed("未传请求参数！");
            }
            String name = detail.getName();
            String pid = detail.getPid();
            if (StringUtils.isBlank(name)){
                return ResponseResult.failed("名称不能为空！");
            }
            DictionaryDetail oldDetail = dictionaryDetailService.getById(detail.getId());
            if (oldDetail == null) {
                return ResponseResult.failed("未查询到相关数据！");
            }
            if (!oldDetail.getName().equals(name) && dictionaryDetailService.isExist("name",name)){
                return ResponseResult.failed("名称”"+name+"“已存在！");
            }
            //检查所添加的上级数据是否存在
            if (StringUtils.isNotBlank(pid)){
                DictionaryDetail dictionaryDetail = dictionaryDetailService.getById(pid);
                if (dictionaryDetail == null) {
                    return ResponseResult.failed("所添加的上级字典不存在！");
                }
            }else {
                detail.setPid(Constants.PARENT_ID);
            }
            boolean update = dictionaryDetailService.updateById(detail);
            return update ? ResponseResult.success() : ResponseResult.updateFailed();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    @ApiOperation(value = "删除")
    @GetMapping("delete")
    @RequiredPermission("dictionaryDetail:delete")
    @RecordLog("删除字典详情")
    public ResponseResult delete(@ApiParam("字典详情主键id") @RequestParam("id") String id){
        try {
            boolean remove = dictionaryDetailService.removeById(id);
            if (remove){
                //删除下级所有数据
                List<DictionaryDetail> detailList = dictionaryDetailService.getByPid(id);
                if (CollectionUtils.isNotEmpty(detailList)){
                    dictionaryDetailService.removeBatchByIds(detailList);
                }
                return ResponseResult.success();
            }
            return ResponseResult.failed();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    @ApiOperation(value = "单个获取")
    @GetMapping("get/{id}")
    public ResponseResult get(@ApiParam("主键id") @PathVariable String id){
        try {
            return ResponseResult.success(dictionaryDetailService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    @ApiOperation(value = "查询单个字典的详情")
    @GetMapping("list")
    public ResponseResult list(@ApiParam("字典ID") @RequestParam("dictionaryId") String dictionaryId){
        try {
            return ResponseResult.success(dictionaryDetailService.getByDictionaryId(dictionaryId));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    @ApiOperation(value = "通过字典编码查询，返回key-value")
    @GetMapping("listByCode")
    public ResponseResult listByCode(@ApiParam("字典编码") @RequestParam("code") String code){
        try {
            List<DictionaryDetail> dictionaryDetailList = dictionaryDetailService.getByDictionaryCode(code);
            List<DictionaryKeyValue> result = new LinkedList<>();
            if (CollectionUtils.isNotEmpty(dictionaryDetailList)){
                dictionaryDetailList.forEach(dictionaryDetail -> {
                    DictionaryKeyValue dictionaryKeyValue = new DictionaryKeyValue();
                    dictionaryKeyValue.setId(dictionaryDetail.getId());
                    dictionaryKeyValue.setKey(dictionaryDetail.getValue());
                    dictionaryKeyValue.setValue(dictionaryDetail.getName());
                    dictionaryKeyValue.setPid(dictionaryDetail.getPid());
                    result.add(dictionaryKeyValue);
                });
            }
            return ResponseResult.success(result);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
}
